﻿using System;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace System.Web.Mvc
{
    /// <summary>
    /// Imported from personal library.  Will create navigation link with class 'active' if link is for current view.
    /// Put into System.Web.Mvc for simple use in Clrizr project
    /// </summary>
    public static class NavigationLinkExtension
    {
        public static MvcHtmlString NavigationLink(this HtmlHelper htmlHelper, string text, string action, string controller)
        {
            bool linkIfCurrent = false;
            string classIfCurrent = "active";
            string currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
            string currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");

            if (!(action == currentAction && controller == currentController))
                return htmlHelper.ActionLink(text, action, controller);

            if (!linkIfCurrent && String.IsNullOrEmpty(classIfCurrent))
                return new MvcHtmlString(text);

            if (!linkIfCurrent)
                return new MvcHtmlString(String.Format("<span class=\"{0}\">{1}</span>", classIfCurrent, text));

            if (!String.IsNullOrEmpty(classIfCurrent))
                return htmlHelper.ActionLink(text, action, controller, null, new { @class = classIfCurrent });

            return htmlHelper.ActionLink(text, action, controller);
        }
    }
}